package net.realsee.service.wcs.work.impl;

import cn.hutool.core.bean.BeanUtil;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.work.WcsWarnClass;
import net.realsee.domain.wcs.work.bo.WcsWarnClassBo;
import net.realsee.domain.wcs.work.vo.WcsWarnClassVo;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.framework.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.mapper.wcs.work.WcsWarnClassMapper;
import net.realsee.service.wcs.work.IWcsWarnClassService;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 告警类别定义Service业务层处理
 *
 * @author realsee
 * @date 2023-08-24
 */
@RequiredArgsConstructor
@Service
public class WcsWarnClassServiceImpl extends ServiceImpl<WcsWarnClassMapper, WcsWarnClass> implements IWcsWarnClassService {

    private final WcsWarnClassMapper baseMapper;

    /**
     * 查询告警类别定义
     */
    @Override
    public WcsWarnClassVo selectVoById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询告警类别定义列表
     */
    @Override
    public TableDataInfo<WcsWarnClassVo> selectVoPage(WcsWarnClassBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsWarnClass> lqw = buildVoQueryWrapper(bo);
        Page<WcsWarnClassVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询告警类别定义列表
     */
    @Override
    public List<WcsWarnClassVo> selectVoList(WcsWarnClassBo bo) {
        LambdaQueryWrapper<WcsWarnClass> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsWarnClass> buildVoQueryWrapper(WcsWarnClassBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsWarnClass> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getWarnClass()), WcsWarnClass::getWarnClass, bo.getWarnClass());
        lqw.like(StringUtils.isNotBlank(bo.getClassName()), WcsWarnClass::getClassName, bo.getClassName());
        return lqw;
    }

    /**
     * 新增告警类别定义
     */
    @Override
    public Boolean insertByBo(WcsWarnClassBo bo) {
        WcsWarnClass add = BeanUtil.toBean(bo, WcsWarnClass.class);
        return this.save(add);
    }

    /**
     * 修改告警类别定义
     */
    @Override
    public Boolean updateByBo(WcsWarnClassBo bo) {
        WcsWarnClass update = BeanUtil.toBean(bo, WcsWarnClass.class);
        return this.updateById(update);
    }

    /**
     * 批量删除告警类别定义
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
